package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ArticleFreemarkerService;
import com.heima.model.article.common.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;

    /**
     * 根据参数加载文章列表数据
     * @param loadType 1 为加载更多 2 为加载最新
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(Short loadType, ArticleHomeDto dto) {

        // 1. 参数校验
        Integer size = dto.getSize();
        if (size == null || size == 0) {
            dto.setSize(10);
        }
        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }

        // 2. 查询数据
        List<ApArticle> list = apArticleMapper.loadArticleList(dto, loadType);

        // 3. 分装结果
        return ResponseResult.okResult(list);
    }

    @Transactional
    @Override
    public ResponseResult save(ArticleDto dto) {
        if (dto == null || StringUtils.isBlank(dto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApArticle apArticle = null;

        // 判断文章id是否存在
        if (dto.getId() == null) {
            // id不存在，保存数据
            // 保存article
            apArticle = new ApArticle();
            BeanUtils.copyProperties(dto, apArticle);
            apArticleMapper.insert(apArticle);

            // 保存article-config
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);

            // 保存article-content
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);

        }else {
            // id存在修改数
            // 根据id查询文章数据
            apArticle = apArticleMapper.selectById(dto.getId());
            if (apArticle != null) {
                BeanUtils.copyProperties(dto, apArticle);
                // 根据Id，更新数据
                apArticleMapper.updateById(apArticle);
            }

            // 根据article_id 查询文章内容数据，在更新数据
            ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery()
                    .eq(ApArticleContent::getArticleId, dto.getId()));
            if (apArticleContent != null) {
                apArticleContent.setContent(dto.getContent());

                apArticleContentMapper.updateById(apArticleContent);
            }
        }


        // 异步生成文章的静态文件
        articleFreemarkerService.buildArticleToMinIO(apArticle, dto.getContent());



        // 需要返回文章id
        return ResponseResult.okResult(apArticle != null ? apArticle.getId() : "");
    }

}
